﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace XmWinKit.Common
{
    public static class SolarTermHelper
    {
        // 24节气名称
        public static readonly string[] SolarTermNames = {
            "小寒", "大寒", "立春", "雨水", "惊蛰", "春分",
            "清明", "谷雨", "立夏", "小满", "芒种", "夏至",
            "小暑", "大暑", "立秋", "处暑", "白露", "秋分",
            "寒露", "霜降", "立冬", "小雪", "大雪", "冬至"
        };

        // 24节气的节气点（以1900年为基准，单位：分钟）
        private static readonly double[] SolarTermInfo = {
            0, 21208, 42467, 63836, 85337, 107014, 128867, 150921, 173149, 195551, 218072, 240693,
            263343, 285989, 308563, 331033, 353350, 375494, 397447, 419210, 440795, 462224, 483532, 504758
        };

        // 获取某年某月某日的节气名（没有则返回空）
        public static string GetSolarTerm(DateTime date)
        {
            int year = date.Year;
            for (int i = 0; i < 24; i++)
            {
                DateTime solarTermDay = GetSolarTermDate(year, i);
                if (solarTermDay.Date == date.Date)
                    return SolarTermNames[i];
            }
            return "";
        }

        // 计算某年某节气的公历日期
        public static DateTime GetSolarTermDate(int year, int index)
        {
            // 1900年1月6日2时5分是小寒
            DateTime baseDate = new DateTime(1900, 1, 6, 2, 5, 0, DateTimeKind.Utc);
            double minutes = 525948.76 * (year - 1900) + SolarTermInfo[index];
            DateTime dt = baseDate.AddMinutes(minutes);
            // 转为本地时间
            return dt.ToLocalTime();
        }
    }
}
